package com.imast.android.bys.adapter;

import java.util.List;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.imast.android.bys.R;
import com.imast.android.bys.io.model.CarroCompraItem;
import com.imast.android.bys.util.VolleySingleton;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CarroCompraAdapter extends ArrayAdapter<CarroCompraItem> {

	private Context context;
	private List<CarroCompraItem> lstItem;
	ImageLoader imageLoader;
	
	public CarroCompraAdapter(Context context, List<CarroCompraItem> lstItem) {
		super(context, R.layout.list_row_carro, lstItem);
		this.context = context;
		this.lstItem = lstItem;
		this.imageLoader = VolleySingleton.getInstance(context).getImageLoader();
		
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.list_row_carro, parent, false);
		View footer = inflater.inflate(R.layout.fragment_carrito, parent, false);
		
		int cantidad = lstItem.get(position).getCantidad();
		double precio = lstItem.get(position).getProducto().getPrecio();
		double total = cantidad * precio;
		
		NetworkImageView thumbNail = (NetworkImageView) rowView.findViewById(R.id.thumbnailItemCarro);
		thumbNail.setImageUrl(lstItem.get(position).getProducto().getThumbilUrl(), imageLoader);
		
		TextView txtTitulo = (TextView) rowView.findViewById(R.id.titleItemCarro);
		txtTitulo.setText(lstItem.get(position).getProducto().getTitulo());
		
		TextView txtCantidad = (TextView) rowView.findViewById(R.id.cantidadItemCarro);
		txtCantidad.setText("Cantidad : " + String.valueOf(cantidad));
		
		TextView txtTotal = (TextView) rowView.findViewById(R.id.totalItemCarro);
		txtTotal.setText("Precio Bruto : " + String.valueOf(total));
		
		if (imageLoader == null)
			imageLoader = VolleySingleton.getInstance(context).getImageLoader();
		
		return rowView;
	}

}
